package com.edu.oracle.controller;

import com.edu.oracle.pojo.Student;
import com.edu.oracle.pojo.ViewStudent;
import com.edu.oracle.service.StudentService;
import com.edu.oracle.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

@WebServlet(name = "StudentController")
public class StudentController extends HttpServlet {
    //常用的，引入的类，使他变成全局变量
    StudentService studentService= new StudentServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理一下，不管是什么请求到转发到doget方法处理
        doGet(request,response);
        //response.sendRedirect("页面重定向");
        //如果用重定向是不可以携带数据给前端
        //用转发能成功吗？
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //0.获取请求路径？？

        //  前端请求完整路径  http://localhost:9090/studentWebSystem_war_exploded/stuList
        //  用内置对象获取到的路径：/studentWebSystem_war_exploded/stuList
        String url=request.getRequestURI();
        System.out.println("截取前的url:"+url);

        //处理要最后一个 / 后的路径字符串  stuList
        url = url.substring(31);//31代表从第31位开始截取字符串
        System.out.println("截取后的url:"+url);//控制台打印输出

        //没有截取则判断写全路径  if(url.equals("/studentWebSystem_war_exploded/stuList")){
        if(url.equals("stuList")){//stuList的servlet，处理查询所用
            //1.获取数据???

            //2.调用service层  新建一个StudentService
            List<Student> studentList =studentService.getStuList();//调用方法studentService的getStuList()方法

            //3.返回数据
            request.setAttribute("stuList",studentList);//用request内置对象把studentList集合数据返回给前端
            request.getRequestDispatcher("stuList.jsp").forward(request,response);
        }
        else if(url.equals("updateStu")){//更新请求获取一个对象数据
            //1.获取前端数据（未必用） 内置对象获取学号，主键
            String sno=request.getParameter("sno");

            //2.请求service业务层处理
            Student student= studentService.selectByOne(sno);

            //3.返回数据给前端
            request.setAttribute("student",student);
            request.getRequestDispatcher("updateStu.jsp").forward(request,response);

        }
        else if(url.equals("viewStudent")) {//视图
            //1.获取数据（不一定）

            //2.调用service业务层实现
            List<ViewStudent> viewStudentList = studentService.viewSelect();
            System.out.println(viewStudentList.getClass());

            //3.返回结果给前端
            request.setAttribute("view",viewStudentList);//把数据传输给前端
            //转发返回到前端页面
            request.getRequestDispatcher("viewStudent.jsp").forward(request,response);
        }
        else if(url.equals("updateStudent")){//修改数据

            Student student =new Student();
            request.setCharacterEncoding("utf-8");

            //1.获取数据
            student.setSno(request.getParameter("sno"));
            student.setSname(request.getParameter("sname"));
            student.setSsex(request.getParameter("ssex"));
            student.setTc(Integer.parseInt(request.getParameter("tc")));
            student.setSpeciality(request.getParameter("speciality"));
            student.setSclass(request.getParameter("sclass"));
            student.setSbirthday(Date.valueOf(request.getParameter("sbirthday")));

            //2.调用service业务层     布尔型：成功返回true,失败返回false
            boolean bl =studentService.updateStudent(student);

            //3.返回结果给前端  //异步请求，需要返回数据给前端用户，提示是否操作成功  js  alert弹出提示
            if(bl){
                System.out.println("更新成功");
                request.getRequestDispatcher("stuList").forward(request,response);//转发stuList查询所有用户的servlet请求
            }else {
                System.out.println("更新失败");
                request.getRequestDispatcher("stuList").forward(request,response);//转发stuList查询所有用户的servlet请求
            }
        }
        else if(url.equals("addStu")){//添加
            System.out.println("添加数据servlet请求处理");

            //1.获取数据
          //自动补全代码： 1.  new Student().val +回车键  2.  ctrl + alt +v

            Student student =new Student();
            request.setCharacterEncoding("utf-8");

            //1.获取数据 存储到实体类（Javabean、model）对象中
            student.setSno(request.getParameter("sno"));//获取学号
            student.setSname(request.getParameter("sname"));//获取姓名
            student.setSsex(request.getParameter("ssex"));//获取性别
            student.setTc(Integer.parseInt(request.getParameter("tc")));//获取总分
            student.setSpeciality(request.getParameter("speciality"));//获取专业
            student.setSclass(request.getParameter("sclass"));//获取班级
            student.setSbirthday(Date.valueOf(request.getParameter("sbirthday")));//获取出生日期

            //2.调用service的addByOne()的方法
            boolean bl =studentService.addByOne(student);
            //3.返回结果
            if(bl){//转发到stuList请求的servlet处理查询所有数据
                request.getRequestDispatcher("stuList").forward(request,response);
            }else {
                request.getRequestDispatcher("stuList").forward(request,response);
            }
        }else if(url.equals("deleteStu")){

            //映射到控制层》》》靠谁？？  servlet  做请求处理，控制
            //1.获取数据 赋值给sno变量
            System.out.println(request.getParameter("sno")  + "学生表删除功能");
            String sno = request.getParameter("sno");

            //2.调用service的deleteStudent删除方法
            studentService.deleteStudent(sno);//service业务层不需要返回数据
            //3.返回结果    转发到查询所有的servlet查询数据
            request.getRequestDispatcher("stuList").forward(request,response);

        }

    }
}
